import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controllers/edit_profile_controller.dart';
import '../widgets/profile_avatar.dart';
import '../../shared/widgets/forms/app_text_field.dart';
import '../../shared/widgets/buttons/app_button.dart';

/// 编辑个人资料视图
class EditProfileView extends GetView<EditProfileController> {
  const EditProfileView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('编辑个人资料'),
        actions: [
          Obx(() => TextButton(
                onPressed: controller.isLoading ? null : controller.saveProfile,
                child: controller.isLoading
                    ? const SizedBox(width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2))
                    : const Text('保存'),
              )),
        ],
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16.0),
        child: Form(
          key: controller.formKey,
          child: Column(
            children: [
              // 头像选择
              _buildAvatarSection(),
              
              const SizedBox(height: 30),
              
              // 姓名输入框
              AppTextField(
                controller: controller.nameController,
                labelText: '姓名/昵称',
                prefixIcon: const Icon(Icons.person_outline),
                validator: controller.validateName,
              ),
              
              const SizedBox(height: 20),
              
              // 邮箱（通常只读）
              AppTextField(
                controller: controller.emailController,
                labelText: '邮箱',
                prefixIcon: const Icon(Icons.email_outlined),
                readOnly: true,
                enabled: false,
              ),
              
              const SizedBox(height: 20),
              
              // 个人简介
              AppTextField(
                controller: controller.bioController,
                labelText: '个人简介',
                hintText: '介绍一下自己吧...',
                prefixIcon: const Icon(Icons.edit_note_outlined),
                maxLines: 3,
                maxLength: 150,
              ),
              
              const SizedBox(height: 40),
              
              // 保存按钮（也可以放在AppBar）
              // Obx(() => AppButton(
              //   text: '保存更改',
              //   onPressed: controller.saveProfile,
              //   isLoading: controller.isLoading.value,
              // )),
            ],
          ),
        ),
      ),
    );
  }

  /// 构建头像选择区域
  Widget _buildAvatarSection() {
    return Column(
      children: [
        Obx(() => ProfileAvatar(
          avatarUrl: controller.avatarUrl, // 网络 URL
          imageFile: controller.selectedAvatarFile, // 本地文件
          userName: controller.nameController.text,
          radius: 50,
          onTap: controller.pickAvatar, // 点击触发选择
        )),
        const SizedBox(height: 8),
        TextButton.icon(
          onPressed: controller.pickAvatar,
          icon: const Icon(Icons.photo_camera_outlined, size: 18),
          label: const Text('更换头像'),
        ),
      ],
    );
  }
} 